function yuv_out = vector_overlay(yuv_in, vec, k, alpha)
%
%blend vector to picture
% overlay_out = vector_overlay(yuv, vec, k, alpha)
% input :
% yuv, x_by_y_by_3 YUV data
% vec, blkx_by_blky_by_2 vector field
% k, factor to vector
% alpha, blender factor, 1: all original pic; 0: all vector map
%
% output:
% yuv_out: picture blender with vector
% example
% yuv_with_vec = vector_overlay(yuv, vec, 1.2, 0.25)

pic_dim = size(yuv_in);
vec_dim = size(vec);
blk_size = [pic_dim(1)/vec_dim(1),pic_dim(2)/vec_dim(2)];


vecy = sqrt(double(vec(:,:,1)).^2 + double(vec(:,:,2)).^2);
vecy = k * vecy;
vecy(vecy>255) = 255;
vecy(vecy<0)   =0;

vecu = k * double(vec(:,:,1))+128;
vecu(vecu>255) = 255;
vecu(vecu<0)   =0;
    
vecv = k * double(vec(:,:,2))+128;
vecv(vecv>255) = 255;
vecv(vecv<0)   =0;

vec_yuv = zeros(size(yuv_in));
vec_yuv(:,:,1) = imresize(vecy, pic_dim(1:2));
vec_yuv(:,:,2) = imresize(vecu, pic_dim(1:2));
vec_yuv(:,:,3) = imresize(vecv, pic_dim(1:2));

yuv_out = vec_yuv*alpha + double(yuv_in)*(1-alpha);
yuv_out(yuv_out>255) = 255;
yuv_out(yuv_out<0)   = 0;
yuv_out = uint8(yuv_out);
